package com.example.kotlindemo.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.kotlindemo.R
import com.example.kotlindemo.adapter.RecyclerviewAdapter
import com.example.kotlindemo.base.BaseActivity
import com.example.kotlindemo.bean.StudentBean
import kotlinx.android.synthetic.main.activity_recycler_view.*

class RecyclerViewActivity : BaseActivity() {
    var adapterrv: RecyclerviewAdapter? = null
    val studentList = ArrayList<StudentBean>()

    companion object {
        fun show(context: Context) {
            context.startActivity(Intent(context, RecyclerViewActivity::class.java))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        bt_shuaxin.setOnClickListener {
            initRecyclerview()
        }
        for (item in 0..100) {
            studentList.add(StudentBean("李明洋", 22, "男"))
        }
        initRecyclerview()
    }

    private fun initRecyclerview() {
        if (adapterrv == null) {
            adapterrv = RecyclerviewAdapter(
                studentList
            ).apply {

                /**
                 * 匿名内部类的实例
                 */
                setOnclickRv(object :
                    RecyclerviewAdapter.Onclick {
                    override fun RvClick(pos: Int) {
                        TODO("Not yet implemented")
                    }

                })
            }
            recyclerview.apply {
                layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
                adapter = adapterrv
            }
        } else {
            studentList.clear()
            adapterrv!!.notifyDataSetChanged()
        }
    }
}